package com.watermeter.android.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.oa.android.R;
import com.watermeter.android.adapter.RouteItemAdapter;
import com.watermeter.android.entry.DownRoute;
import com.watermeter.android.entry.Rows;

import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;


/**
 * 待抄表列表
 * 
 * @author -zouqh
 * 
 */
public class ScanSelectActivity extends Activity implements OnClickListener {

	private ImageView close;
	private TextView title;
	private ListView listView;
	private RouteItemAdapter adapter;
	private List<DownRoute> downRoutelist;
	public List<DownRoute> downRoutelist1;
	public DbManager.DaoConfig daoConfig;
	private String routeId;
    public int flag=0;//判断是否保存成功，成功后需要刷新界面
	public static ScanSelectActivity activity;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		activity=this;
		setContentView(R.layout.olne_select_activity);
		close = (ImageView) findViewById(R.id.close);
		title = (TextView) findViewById(R.id.title_text);
		listView=(ListView)findViewById(R.id.listview);
		title.setText("修改信息");
		close.setOnClickListener(this);
		routeId=getIntent().getStringExtra("id");
//		getDB();
        daoConfig = new DbManager.DaoConfig();
		getdownDate();
        System.out.println("routeId===="+routeId);
		if(null!=downRoutelist&&downRoutelist.size()>0){
			downRoutelist1=new ArrayList<>();
			downRoutelist1.addAll(downRoutelist);
			adapter=new RouteItemAdapter(this,downRoutelist1);
			listView.setAdapter(adapter);

		}else {
			Toast.makeText(this,"暂时没有数据！",Toast.LENGTH_SHORT).show();
		}




	}

    @Override
    protected void onResume() {
        super.onResume();
        if (flag!=0){
            flag=0;
            DcbActivity.dcbActivity.isFlash=1;
            getdownDate();
            adapter.notifyDataSetChanged();
            getScbNmu();
        }
    }

    @Override
	public void onClick(View v) {
		// TODO Auto-generated method stub

		Intent intent ;
		switch (v.getId()) {
		case R.id.close:
			finish();
			break;

		default:
			break;
		}
	}


	/**
	 * 获取已经下载的路线
	 */
	private void getdownDate() {
//
		DbManager manager = x.getDb(daoConfig);
		try {
			if (routeId!=null&&routeId.length()>0){
				downRoutelist= manager.selector(DownRoute.class).where("CBLINE","=",routeId).findAll();
				System.out.println("downRoutelist.size()="+downRoutelist.get(0));
			}



		} catch (DbException e) {
			e.printStackTrace();
		}
	}

	private void selectName(String str){

		downRoutelist1.clear();
		for (int i=0;i<downRoutelist.size();i++){
			if (downRoutelist.get(i).getUSERNAME().contains(str)){
				downRoutelist1.add(downRoutelist.get(i));
			}
		}
		adapter.notifyDataSetChanged();

	}


    /**
     * 获取实抄表条数
     */
    private void getScbNmu() {

        DbManager manager = x.getDb(daoConfig);
       long num=0;
        try {
            if (routeId!=null&&routeId.length()>0){
                num= manager.selector(DownRoute.class).
                        where("CBLINE","=",routeId).and("change","=",1).count();
                System.out.println("num=========="+num);
               List<Rows>   rowsList= manager.selector(Rows.class).where("ID","=",routeId).findAll();
                System.out.println("rowsList=========="+rowsList.size());
                for(int i=0;i<rowsList.size();i++){
                    Rows rows=rowsList.get(i);
                    rows.setSCBNUM((int)num);
                    manager.saveOrUpdate(rows);
                }
            }

            if(num>0){

            }



        } catch (DbException e) {
            e.printStackTrace();
        }
    }


}
